<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inform:add']"
          >{{ $t("add") }}</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="informList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('employer')"
        align="center"
        prop="employerName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('notification_type')"
        align="center"
        prop="noticeType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_notice_type"
            :value="scope.row.noticeType"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('trigger_event')"
        align="center"
        prop="triggerEvent"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.trigger_event"
            :value="scope.row.triggerEvent"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('notification_method')"
        align="center"
        prop="noticeMethod"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.service_notice_type"
            :value="scope.row.noticeMethod"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('system_message_template')"
        align="center"
        prop="templateContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('status')"
        align="center"
        prop="status"
        :show-overflow-tooltip="true"
      >
        <template scope="{row}">
          <div v-if="row.noticeMethod == 3">
            {{
              row.status == 0
                ? "审核中"
                : row.status == 1
                ? "通过"
                : row.status == 2
                ? "拒绝"
                : row.status == 10
                ? "审核取消"
                : ""
            }}
          </div>
          <span v-else> -</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['inform:edit']"
            >{{ $t("modify") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
            v-hasPermi="['inform:delete']"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改通知信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item
          :label="$t('select_employing_company')"
          prop="employerId"
          disabled
        >
          <el-select
            clearable
            v-model="form.employerId"
            :placeholder="$t('select_employer')"
            multiple
            style="width: 100%"
          >
            <el-option
              v-for="item in getAllEmployerList"
              :key="item.id"
              :label="item.employerName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('notification_type')" prop="noticeType">
          <el-select
            v-model="form.noticeType"
            :placeholder="
              $t('please_select', { name: $t('notification_type') })
            "
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_notice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trigger_event')" prop="triggerEvent">
          <el-select
            v-model="form.triggerEvent"
            :placeholder="
              $t('please_select', { name: $t('please_select', { name: '' }) })
            "
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.trigger_event"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('notification_method')" prop="noticeMethod">
          <el-select
            v-model="form.noticeMethod"
            :placeholder="
              $t('please_select', { name: $t('notification_method') })
            "
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.service_notice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('system_message_template')"
          prop="templateContent"
        >
          <el-input
            v-model="form.templateContent"
            :placeholder="
              $t('please_enter', { name: $t('system_message_template') })
            "
            type="textarea"
            :rows="5"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInform,
  getInform,
  delInform,
  addInform,
  updateInform,
  checkSmsTemplateCode,
} from "@/api/custom/inform";
import { getAllEmployer } from "@/api/custom/employerList";

export default {
  name: "Inform",
  dicts: ["sys_notice_type", "trigger_event", "service_notice_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知信息表格数据
      informList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        employerId: null,
        noticeType: null,
        triggerEvent: null,
        noticeMethod: null,
        templateContent: null,
      },
      // 表单校验
      rules: {},
      // 雇主集合
      getAllEmployerList: [],
    };
  },
  created() {
    this.getList();
    this.getAllEmployer();
  },
  methods: {
    /** 查询通知信息列表 */
    getList() {
      this.loading = true;
      checkSmsTemplateCode().then(() => {
        listInform(this.queryParams).then((response) => {
          this.informList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        employerId: null,
        noticeType: null,
        triggerEvent: null,
        noticeMethod: null,
        templateContent: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t("add");
    },
    // 获取雇主集合
    getAllEmployer() {
      getAllEmployer().then((response) => {
        this.getAllEmployerList = response.data;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInform(id).then((response) => {
        this.form = response.data;
        this.form.employerId = this.form.employerId.split(",");
        this.open = true;
        this.title = this.$t("modify");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            let data = JSON.parse(JSON.stringify(this.form));
            data.employerId = data.employerId.toString();
            updateInform(data).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          } else {
            let data = JSON.parse(JSON.stringify(this.form));
            data.employerId = data.employerId.toString();
            addInform(data).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delInform(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/inform/export",
        {
          ...this.queryParams,
        },
        `inform_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
